package com.example.listener.listener;

import com.example.listener.jpa.OrderRepository;
import com.example.listener.pojo.Order;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class DlxListener {

    @Autowired
    private OrderRepository orderRepository;

    @RabbitListener(queues = "queue.order.dlx")
    public void listener(@Payload String massage){
        System.out.println("order_no : " + massage);
        Order order = orderRepository.findByOrderNo(massage);
        if(order == null){
            return;
        }
        if(order.getStatus().equals(Order.STATUS_UNPAID)){
            order.setStatus(Order.STATUS_EXPIRED);
            order.setUpdateTime(new Date());
            orderRepository.save(order);
        }
    }
}
